package com.example.leetcode.solution;

import com.example.leetcode.ListNode;

public class Solution203 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1,new ListNode(6,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5, new ListNode(6)))))));
        new Solution203().removeElements(head , 6);
    }

    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        ListNode newHead = null;
        ListNode temp = null;
        while(head != null){
            if(head.val != val){
                if(newHead == null){
                    newHead = new ListNode(head.val);
                    temp = newHead;
                }else{
                    ListNode temp2 = temp;
                    temp = new ListNode(head.val);
                    temp2.next = temp;
                }
            }
            head = head.next;
        }
        return newHead;
    }
}
